# -*- coding: utf-8 -*-
# 摄像头头像识别
import time
import datetime

import face_recognition
import cv2
import sys

from models.attendce import Attendce
from models.user import User
from utils.mqttutils import publih_msg

video_capture = cv2.VideoCapture(0)
user_model = User()

user_list = user_model.user_list()

if len(user_list)==0:
    print("system have not user")
    sys.exit(0)
    
# Create arrays of known face encodings and their names
# 脸部特征数据的集合
known_face_encodings = []

# 人物名称的集合
known_face_names = []
known_face_ids = {}


open_time = time.time() - 10

for user in user_list:
    image_path = user.get("image_path")
    viewname = user.get("username")
    id=  user.get("id")
    image = face_recognition.load_image_file(image_path)
    face_encoding = face_recognition.face_encodings(image)[0]
    known_face_encodings.append(face_encoding)
    known_face_names.append(viewname)
    known_face_ids[viewname] = id

face_locations = []
face_encodings = []
face_names = []
process_this_frame = True
attenduce = Attendce()

while True:
    # 读取摄像头画面
    ret, frame = video_capture.read()
    
    # 改变摄像头图像的大小，图像小，所做的计算就少
    small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)
    
    # opencv的图像是BGR格式的，而我们需要是的RGB格式的，因此需要进行一个转换。
    rgb_small_frame = small_frame[:, :, ::-1]
    
    # Only process every other frame of video to save time
    if process_this_frame:
        # 根据encoding来判断是不是同一个人，是就输出true，不是为flase
        face_locations = face_recognition.face_locations(rgb_small_frame)
        face_encodings = face_recognition.face_encodings(rgb_small_frame, face_locations)
        
        face_names = []
        for face_encoding in face_encodings:
            # 默认为unknown
            matches = face_recognition.compare_faces(known_face_encodings, face_encoding,tolerance=0.34)
            name = "Unknown"
            
            # if match[0]:
            #     name = "michong"
            # If a match was found in known_face_encodings, just use the first one.
            if True in matches:
                first_match_index = matches.index(True)
                name = known_face_names[first_match_index]
            face_names.append(name)
    
    now_time = time.time()
    if now_time - open_time > 10:
        try:
            publih_msg(topic="FaceId", msg="OFF")
            open_time = now_time
        except Exception as e:
            print(e)
    
    process_this_frame = not process_this_frame
    
    # 将捕捉到的人脸显示出来
    for (top, right, bottom, left), name in zip(face_locations, face_names):
        # Scale back up face locations since the frame we detected in was scaled to 1/4 size
        top *= 4
        right *= 4
        bottom *= 4
        left *= 4
        
        if name != "Unknown":
            color = (255, 0, 0)
            msg = "ON"
        else:
            color = (0, 0, 255)
            msg="OFF"
        
        # 矩形框
        cv2.rectangle(frame, (left, top), (right, bottom), color, 2)
        
        # 加上标签
        cv2.rectangle(frame, (left, bottom - 35), (right, bottom), color, cv2.FILLED)
        font = cv2.FONT_HERSHEY_DUPLEX
        cv2.putText(frame, name, (left + 6, bottom - 6), font, 1.0, (255, 255, 255), 1)
        try:
            print("认证结果：{}".format(name))
            publih_msg(topic="FaceId", msg=msg)
            attenduce.attendce_create(user_id=known_face_ids[name],attendce_date=datetime.date.today(),attendce_time=datetime.datetime.now(),not_attendce=False)
            open_time = time.time()
        except Exception as e:
            print(e)
    # Display
    cv2.imshow('FaceId', frame)
    
    # 按Q退出
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

video_capture.release()
cv2.destroyAllWindows()
